package com.ysx.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ysx.common.constants.BusinessConstants;
import com.ysx.common.pojo.PageInfo;
import com.ysx.common.pojo.PageRequestDto;
import com.ysx.exception.ToutiaoException;
import com.ysx.wemedia.dto.ContentNode;
import com.ysx.wemedia.dto.WmNewsDto;
import com.ysx.wemedia.dto.WmNewsDtoSubmit;
import com.ysx.wemedia.mapper.WmNewsMapper;
import com.ysx.wemedia.pojo.WmNews;
import com.ysx.wemedia.service.WmNewsService;
import com.ysx.wemedia.vo.WmNewsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 自媒体图文内容信息表 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2021-09-08
 */
@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    //根据新闻ID查询记录 回显
    @Override
    public WmNewsDtoSubmit<ContentNode> obtainVmNewsDtoSubmit(String vmNewsId) throws JsonProcessingException, ToutiaoException {
        WmNewsDtoSubmit<ContentNode> wmNewsDtoSubmit = new WmNewsDtoSubmit<>();
        //根据新闻Id 查询到记录
        WmNews wmNewsDB = getById(vmNewsId);
        if (Objects.isNull(wmNewsDB)) {
            throw new ToutiaoException("文章id异常，请核对数据库是否存在此记录");
        }
        //拷贝属性
        BeanUtils.copyProperties(wmNewsDB,wmNewsDtoSubmit);
        //修改images属性重新赋值
        List<String> imagesList = Arrays.asList(wmNewsDB.getImages().split(","));
        wmNewsDtoSubmit.setImages(imagesList);
        //修改content属性重新赋值
        String content = wmNewsDB.getContent();
        List<ContentNode> contentNodeList = objectMapper.readValue(content, List.class);
        wmNewsDtoSubmit.setContent(contentNodeList);
        return wmNewsDtoSubmit;
    }
    //根据请求条件 查询审核失败或者人工审核自媒体素材
    @Override
    public PageInfo<WmNewsVo> getSearchNotPass(PageRequestDto<WmNewsDto> pageRequestDto) {
        //select wm.*,us.id as authorName  from wm_news wm,wm_user us where wm.user_id =us.id and wm.`status` BETWEEN 2 and 3 and title like concat('%',#{searchKeyWord},'%') limit #{page},#{size}

        //获取分页条件
        Long page = pageRequestDto.getPage();
        Long size = pageRequestDto.getSize();
        WmNewsDto searchKeyWord = pageRequestDto.getBody();
        //调用Mapper方法查询
        List<WmNewsVo> WmNewsVoList=wmNewsMapper.getSearchNotPass(page,size,searchKeyWord);
        //查询所有记录
        Long count=wmNewsMapper.CountNotPassNum(searchKeyWord);
        Long countPage=count%size==0?(count/size):(count/size+1);
        //封装对象
        PageInfo<WmNewsVo> pageInfo=new PageInfo<>();
        pageInfo.setSize(size);
        pageInfo.setPage(page);
        pageInfo.setTotal(count);
        pageInfo.setTotalPages(countPage);
        pageInfo.setList(WmNewsVoList);
        return pageInfo;
    }
    //根据文章id联表查询文章信息和作者用户
    @Override
    public WmNewsVo findWmNewsStatus(Integer wmNewsId) {

        WmNewsVo wmNewsVo= wmNewsMapper.findWmNewsStatus(wmNewsId);

        return wmNewsVo;
    }


    //根据文章的id删除
    @Override
    public void delVmNewsDtoSubmit(String delVmNews) throws ToutiaoException {
        //删除限制  文章状态为9 且已上架的记录不能删除

        //根据新闻Id 查询到记录
        WmNews wmNewsDB = getById(delVmNews);
        if (Objects.isNull(wmNewsDB)) {
            throw new ToutiaoException("文章id异常，请核对数据库是否存在此记录");
        }
        //获取Status/enable状态
        Integer status = wmNewsDB.getStatus();
        Integer enable = wmNewsDB.getEnable();
        //判断条件
        if (status == 9 && enable == 1) {
            throw new ToutiaoException("删除失败,文章已上架");
        }
        else {
            this.removeById(delVmNews);
        }
    }
    //根据文章的id上下架作品
    @Override
    public void outVmNewsDtoSubmit(String outVmNews) throws ToutiaoException {
        //根据新闻Id 查询到记录
        WmNews wmNewsDB = getById(outVmNews);
        if (Objects.isNull(wmNewsDB)) {
            throw new ToutiaoException("文章id异常，请核对数据库是否存在此记录");
        }
        //获取Status/enable状态
        Integer status = wmNewsDB.getStatus();
        if (status == 9) {
            throw new ToutiaoException("已上架的作品不能下架！");
        } else {
            wmNewsDB.setStatus(0);
            boolean update = this.updateById(wmNewsDB);
            if (update) {//更新成功 发送异步消息 修改文章状态
                HashMap<String, String> msgMap = new HashMap<>();
                msgMap.put("type",String.valueOf(status));
                msgMap.put("articleId",String.valueOf(wmNewsDB.getArticleId()));
                //发送消息
                kafkaTemplate.send(BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_ADMIN_MQTOPIC, JSON.toJSONString(msgMap));
            }
        }
    }

}
